The present invention relates generally to data processing devices, and specifically to data masking in such devices.
Data masking is used in a wide range of processing applications. Given an input data field made up of a predetermined number of bits, the data mask specifies a group of the bits to be selected by a processor (usually a contiguous group of bits, although not necessarily so). Typically, the mask is held in memory as a data word of the same length as the input data field, with ones in the positions that correspond to the bits that are to be selected, and zeroes elsewhere. By performing a bitwise xe2x80x9cANDxe2x80x9d operation between the input field and the mask, the processor can extract the contents of the group of bits from the input field that is specified by the mask. Applications of data masking including setting and resetting fields of a memory address of an input/output device; arithmetic operations, such as binary modulus and truncation; and processing of header fields of packets in digital communication devices, such as protocol converters.
When data masking is used, each processor command that is stored in program memory is typically accompanied by a corresponding mask. As the processor reads operation codes (op-codes) from the program memory for execution, it also reads the appropriate mask together with each of the op-codes. The simplest way to store the mask is as an explicit mask word, equal in length to the data fields that are to be processed. This approach becomes very wasteful of bit count when the fields are long. An alternative approach is to pass to the processor only the start and end bits of the selected group of bits that is specified by the mask. This solution, however, requires that the processor implement a complicated formula or state machine in order to reproduce the mask. It is therefore impractical for use in simple, high-speed processors, such as dedicated processors that are used in communication cores and protocol converters. The solution based on passing only start and end bits is also inherently limited to defining contiguous mask fields.
Preferred embodiments of the present invention provide an improved method and format for encoding data masks. The format has the advantage of being simple to implement and decode, while reducing substantially the number of bits required to represent and store the mask.
In preferred embodiments of the present invention, a data mask is divided into a predetermined number of segments of equal lengths for encoding. Each segment is represented by a unique segment code, depending on its content: Segments that contain only ones (typically segments that are entirely within the group of bits to be selected in accordance with the mask) receive one segment code; segments that contain only zeroes receive a second segment code; and segments containing a transition from zeroes to ones or from ones to zeroes receive third and fourth segment codes, respectively. The third segment code thus identifies the segment that contains the left end of the selected group of bits in the mask, while the fourth segment code identifies the segment containing the right end. Typically, each of the segment codes is two bits long. A coded representation of the entire mask comprises the sequence of segment codes along with the full contents of the segments indicated by the third and fourth codes. A 64-bit data mask, for example, can be divided into eight segments and represented in this way by a 32-bit mask code (eight two-bit segment codes plus the full contents of two eight-bit segments).
To reconstruct the mask, a processor reconstructs the original segments based on the segment codes. On this basis, it concatenates fields of ones or zeroes with the contents of the segments indicated by the third and fourth codes, in the order dictated by the segment codes. No logical operations are required beyond selecting the appropriate fields. This method of encoding and decoding is therefore particularly useful in systems in which it is not desirable to use a microprocessor, such as in communication cores or protocol conversion chips.
There is therefore provide, in accordance with a preferred embodiment of the present invention, a method for encoding a data mask that consists of a given total number of bits and includes a selected group of contiguous bits within the total number, the selected group having a left end and a right end, the method including:
dividing the data mask into a plurality of segments;
representing the segments by respective segment codes, each code indicating whether the bits in the respective segment fall entirely outside the selected group, entirely within the selected group, or include the left end or the right end of the group; and
combining the segment codes so as to generate a mask code, which can be decoded to reconstruct the data mask.
Preferably, dividing the data mask into the plurality of segments includes dividing the mask into segments of equal lengths. Most preferably, dividing the mask into the segments of equal lengths includes dividing the mask into segments whose lengths are at least approximately equal to the square root of the total number of bits in the mask. Additionally or alternatively, representing the segments by respective segment codes includes representing each of the segments by a two-bit code.
Further preferably, combining the segment codes includes combining the segment codes with an indication of the bits in the segments that include the left end and the right end of the selected group. Most preferably, combining the segment codes with the indication of the bits includes concatenating the segment codes with the bits in the segments that include the left end and the right end of the selected group.
In a preferred embodiment, the bits in the selected group are ones, while the bits in the data mask that are not in the selected group are zeroes. In another preferred embodiment, the bits in the selected group are zeroes, while the bits in the data mask that are not in the selected group are ones.
Preferably, the method includes storing the mask code in a memory, and conveying the code from the memory to a processor for decoding of the mask code to reconstruct the data mask and application of the reconstructed data mask to process an input field.
There is also provided, in accordance with a preferred embodiment of the present invention, a device for processing an input field of data using a mask that consists of a given total number of bits and includes a selected group of contiguous bits within the total number, the selected group having a left end and a right end, the device including:
a memory, configured to store the mask in an encoded form generated by dividing the mask into a plurality of segments, representing the segments by respective segment codes, each code indicating whether the bits in the respective segment fall entirely outside the selected group, entirely within the selected group, or include the left end or the right end of the group, and combining the segment codes so as to generate a mask code; and
a processor, coupled to read the mask code from the memory and operative to decode the mask code so as to reconstruct the mask and to apply the reconstructed mask to process the input field.
Preferably, the processor includes a multiplexer, which is arranged to select the bits to be included in each of the segments of the reconstructed mask, responsive to the segment codes.
In a preferred embodiment, the device is coupled to receive and process data packets in a communication network, and the input field includes a header field of one of the packets.
The present invention will be more fully understood from the following detailed description of the preferred embodiments thereof, taken together with the drawings in which: